package com.ymq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ymq.reggie.common.R;
import com.ymq.reggie.dto.DishDto;
import com.ymq.reggie.dto.SetmealDto;
import com.ymq.reggie.entity.Category;
import com.ymq.reggie.entity.Setmeal;
import com.ymq.reggie.entity.SetmealDish;
import com.ymq.reggie.service.CategoryService;
import com.ymq.reggie.service.SetmealDishService;
import com.ymq.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐保存
     * @param dto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto dto){
        log.info("dto:{}",dto);
      setmealService.saveWithDish(dto);


        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        lambdaQueryWrapper.orderByAsc(Setmeal::getUpdateTime);

        Page<SetmealDto> page2 = new Page<>(page,pageSize);

        setmealService.page(page1,lambdaQueryWrapper);
        BeanUtils.copyProperties(page1,page2,"records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> collect = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            String name1 = categoryService.getById(categoryId).getName();
            setmealDto.setCategoryName(name1);
            return setmealDto;


        }).collect(Collectors.toList());
        page2.setRecords(collect);


        return R.success(page2);

    }

    /**
     * 套餐删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.deleteWithDish(ids);


        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status,@RequestParam List<Long> ids){
        log.info("ids:{},status:{}",ids,status);
      //  String name=status==0?"停售":"起售";
        List<Setmeal> collect = ids.stream().map((item) -> {
            Setmeal setmeal = setmealService.getById(item);
            setmeal.setStatus(status);
            return setmeal;
        }).collect(Collectors.toList());
        setmealService.saveOrUpdateBatch(collect);
     if (status==0){
         return R.success("该套餐停售成功");
     }

        return R.success("该套餐起售成功");
    }
    /*
    套餐分类查询
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lambdaQueryWrapper.orderByAsc(Setmeal::getCreateTime);
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);


        return R.success(list);
    }

    /**
     * 修改套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> update(@PathVariable Long id){
        log.info("id信息：{}",id);
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> setmealDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDtoLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(setmealDtoLambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);

        return  R.success(setmealDto);
    }

    @PutMapping
    public R<String> updateOk(@RequestBody SetmealDto setmealDto){
        //删除套餐的菜品
        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//                stream().map((item)->{
//            item.setSetmealId(id);
//            return item;
//        }).collect(Collectors.toList());


        List<SetmealDish> setmealDishList = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
        setmealService.saveOrUpdate(setmealDto);


        return R.success("修改成功");
    }
}

